package com.yanqu.road.server.cmd.cricket.court.cross;

import com.yanqu.road.entity.cricket.config.CricketCourtConfig;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cricket.CricketProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.gameplayer.module.cricket.CricketMgr;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;
import java.util.List;

@Cmd(code = Protocol.S_CRICKET_SEND_RANK_REWARD_FROM_CROSS, desc = "蛐蛐跨服发奖励来")
public class CricketSendRankRewardFromCross implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        CricketProto.CrossSendCricketCourtRankRewardMsg respMsg = CricketProto.CrossSendCricketCourtRankRewardMsg.parseFrom(packet.getBytes());
        List<CricketProto.CricketCourtRankUserTempMsg> userList = respMsg.getUserList();
        for (CricketProto.CricketCourtRankUserTempMsg userTemp : userList) {
            long serverId = userTemp.getServerId();
            if (serverId == GameServer.getInstance().getServerId()) {
                long userId = userTemp.getUserId();
                int courtId = userTemp.getCourtId();
                int rank = userTemp.getRank();
                // 处理下奖励
                String rewardStr = CricketMgr.getConfig().getCricketCourtConfig(courtId).getRankReward();
                String reward = "";
                List<String> rewardStrList = StringUtils.stringToStringList(rewardStr, "#");
                for (String rewardParamStr : rewardStrList) {
                    List<String> rewardParamList = StringUtils.stringToStringList(rewardParamStr, "\\|");
                    int minRank = Integer.parseInt(rewardParamList.get(0));
                    int maxRank = Integer.parseInt(rewardParamList.get(1));
                    if (minRank <= rank && maxRank >= rank) {
                        reward = rewardParamList.get(2);
                        break;
                    }
                }
                // 【【5.7.0】【测试】【促织竞技场】上榜用户，未达到领奖条件，会发空邮件，不要补发】https://www.tapd.cn/67122645/bugtrace/bugs/view?bug_id=1167122645001044022
                // 奖励为空说明该名次没配奖励，则不发奖
                if (StringUtils.isNullOrEmpty(reward)) {
                    continue;
                }
                // 发邮件
                String language = UserMgr.getLanguage(userId);
                String courtName = "";
                CricketCourtConfig courtConfig = CricketMgr.getConfig().getCricketCourtConfig(courtId);
                if (courtConfig != null) {
                    courtName = courtConfig.getCourtName();
                }
                String mailTitle = MultipleLanguageMgr.getContent(MailManager.CRICKET_FIGHT_REWARD_TITLE, language);
                String mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.CRICKET_FIGHT_REWARD_CONTENT, language), MultipleLanguageMgr.getContent(courtName, language), rank);
                MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), reward, mailContent, mailTitle);
            }
        }
    }
}
